package com.misolab.utility.speedmate.util;

import java.util.ArrayList;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.misolab.utility.speedmate.R;

public 	class Control implements OnItemSelectedListener, OnClickListener{
	
	public interface Lisenter{
		public void onClickInitialize(Control control);
		public void onClickPreview(Control control);
		public void onSelected(Control control);
	}
	
	Lisenter listener;
	public void setLisenter(Lisenter listener){
		this.listener = listener;
	}
	
	Activity activity;
	int selected;
	final int STRING_ID = R.string.very_slow;
	
	Gallery gallery;
	ImageView icon;
	TextView desc;
	Button init;
	Button preview;
	
	ArrayList<String> listDescription = new ArrayList<String>();
	ArrayList<Integer> listIcon = new ArrayList<Integer>();
	
	public Control(Activity activity, int controlID, int selected) {
		super();
		this.activity = activity;
		this.selected = selected;
		
		//	description
		listDescription.add(activity.getString(R.string.very_slow));
		listDescription.add(getString(R.string.slow));
		listDescription.add(getString(R.string.normal));
		listDescription.add(getString(R.string.fast));
		listDescription.add(getString(R.string.very_fast));
		
		listIcon.add(R.drawable.icon_vs);
		listIcon.add(R.drawable.icon_s);
		listIcon.add(R.drawable.icon_n);
		listIcon.add(R.drawable.icon_f);
		listIcon.add(R.drawable.icon_vf);
		
		LinearLayout ll = (LinearLayout) activity.findViewById(controlID);
		
		icon = (ImageView) ll.findViewById(R.id.icon_value);
		desc = (TextView) ll.findViewById(R.id.desc_value);
		gallery = (Gallery) ll.findViewById(R.id.gallery);
		gallery.setAdapter(new StartAdapter(activity));
		gallery.setOnItemSelectedListener(this);
		gallery.setSelection(selected, true);
		
		init = (Button) ll.findViewById(R.id.button_init);
		init.setOnClickListener(this);
		preview = (Button) ll.findViewById(R.id.button_preview);
		preview.setOnClickListener(this);
	}
	
	private String getString(int resId) {
		return activity.getString(resId);
	}


	public int getSelected(){
		return selected;
	}
	
	public void setSelected(int selected, boolean anaimate){
		this.selected = selected;
		gallery.setSelection(selected, anaimate);		
	}
	
	public int getIndexFromSelected(){
		int retValue = 0;
		if (selected <= 3) {
			retValue = 0;
		}
		else if (selected <= 7) {
			retValue = 1;
		}
		else if (selected <= 12) {
			retValue = 2;
		}
		else if (selected <= 16) {
			retValue = 3;
		}
		else {
			retValue = 4;
		}
		return retValue;
	}
	
	public String getSelectedString(){
		String retValue = listDescription.get(getIndexFromSelected());
		return retValue;
	}
	
	public int getSelectedIcon(){
		int retValue = listIcon.get(getIndexFromSelected());
		return retValue;			
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		selected = position;
		icon.setImageResource(getSelectedIcon());
		desc.setText(getSelectedString());
		
		if (listener != null) {
			listener.onSelected(this);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.button_init) {
			if (listener != null) {
				listener.onClickInitialize(this);
			}
		}else if(v.getId() == R.id.button_preview){
			if (listener != null) {
				listener.onClickPreview(this);
			}
		}
	}

}
